<?php


namespace app\modules\Admin\models;


use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;

class agentgoodslogModel extends Model
{
    protected $table = 'agent_goods_log';
    protected $primaryKey = 'goods_id';
    protected $appends = ['oper_name', 'event_name'];
    protected $hidden = [];


    public function getOperTypeAttribute()
    {
        // admin=管理员,system=系统,user=用户
        switch ($this->attributes['oper_type']) {
            case 'admin':
                return '管理员';
            case 'system':
                return '系统';
            case 'user':
                return '用户';
            default:
                return '未知';
        }
    }

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getOperNameAttribute()
    {
        // admin=管理员,system=系统,user=用户
        switch ($this->attributes['oper_type']) {
            case 'admin':
                $name = \adminModel::query()->where('id', $this->attributes['oper_id'])->value('username');
                return $name?:'未知';
            case 'system':
                return '系统';
            case 'user':
                $name = \memberModel::query()->where('id', $this->attributes['oper_id'])->value('nickname');
                return $name?:'未知';
            default:
                return '未知';
        }
    }

    public function getEventNameAttribute()
    {
        // 事件标识:add=增加, edit=编辑, del=删除
        switch ($this->attributes['event']) {
            case 'add':
                return '新增';
            case 'edit':
                return '更新';
            case 'del':
                return '删除';
            default:
                return '未知';
        }
    }

    public function goods()
    {
        return $this->belongsTo(\goodsModel::class, 'goods_id', 'id');
    }
}